{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7eab4bf4",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "05b23782",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['已完成(评估)', 2020, 'A类', '宁波维裕生物科技有限公司', '东', 'E2', '非直服', 31.92,\n",
       "        9.0, 4.3, 8, 4.5, 7.5, 7.2, 72.42],\n",
       "       ['已完成(评估)', 2020, 'A类', '杭州迈道生物技术有限公司', '东', 'E2', '非直服', 45.0,\n",
       "        9.5, 4.1, 8, 5.0, 10.0, 8, 89.6],\n",
       "       ['已完成(评估)', 2020, 'A类', '华东医药股份有限公司器材化剂分公司', '东', 'E2', '非直服',\n",
       "        26.33, 9.0, 4.1, 6, 5.0, 10.0, 8, 68.43],\n",
       "       ['已完成(评估)', 2020, 'A类', '天津慧康百泰科技发展有限公司', '北', 'N3', '非直服', 12.46,\n",
       "        8.0, 4.0, 6, 4.0, 8.0, 8.3, 50.76],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '海尔施生物医药股份有限公司', '东', 'E2', '非直服',\n",
       "        34.78, 9.5, 4.8, 10, 3.5, 14.5, 9, 86.08],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '河北博世林科技有限公司', '北', 'N3', '非直服', 34.07,\n",
       "        6.5, 4.0, 8, 4.5, 9.0, 9, 75.07],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '北京美康百泰医药科技有限公司', '北', 'N1', '非直服',\n",
       "        34.61, 10.0, 5.0, 9, 5.0, 9.5, 9.2, 82.31],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '北京东方瑞澳医疗设备有限公司', '北', 'N1', '非直服',\n",
       "        31.38, 10.0, 5.0, 6, 5.0, 9.0, 9.8, 76.18],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '上海益健医学服务中心', '东', 'E1', '直服', 43.99,\n",
       "        20.0, 4.5, 7, 5.0, 11.5, '不适用', 91.99],\n",
       "       ['已完成(评估)', 2020, 'A类', '国药集团深圳医疗器械有限公司', '南', 'S1', '直服', 39.97,\n",
       "        18.0, 3.8, 8, 3.0, 10.0, '不适用', 82.77],\n",
       "       ['已完成(评估)', 2020, 'A类', '南昌普健实业有限公司', '南', 'S3-JX', '直服', 35.84,\n",
       "        18.0, 4.2, 7, 5.0, 10.0, '不适用', 80.04],\n",
       "       ['已完成(评估)', 2020, 'A类', '茂名市华宇医疗器械有限公司', '南', 'S1', '直服', 35.75,\n",
       "        16.0, 3.1, 9, 2.0, 10.0, '不适用', 75.85],\n",
       "       ['已完成(评估)', 2020, 'A类', '上海威豪医疗科技有限公司', '东', 'E1', '直服', 37.32,\n",
       "        16.0, 3.4, 8, 4.5, 6.0, '不适用', 75.22],\n",
       "       ['已完成(评估)', 2020, 'A类', '广东安利康药业有限公司', '南', 'S1', '直服', 32.04,\n",
       "        15.0, 2.1, 8, 3.0, 7.5, '不适用', 67.64],\n",
       "       ['已完成(评估)', 2020, 'A类', '国药控股医疗器械有限公司', '东', 'E1', '直服', 21.78,\n",
       "        17.0, 3.5, 10, 4.0, 10.0, '不适用', 66.28],\n",
       "       ['已完成(评估)', 2020, 'A类', '佛山市南海凯威医疗器械有限公司', '南', 'S1', '直服', 39.64,\n",
       "        11.0, 2.7, 6, 1.5, 10.0, '不适用', 70.84],\n",
       "       ['已完成(评估)', 2020, 'A类', '上海衡立医疗设备有限公司', '东', 'E1', '直服', 36.2,\n",
       "        20.0, 5.0, 8, 5.0, 9.0, '不适用', 83.2],\n",
       "       ['已完成(评估)', 2020, 'A类', '内蒙古盛德医疗器械有限公司', '北', 'N2', '直服', 25.7,\n",
       "        20.0, 5.0, 7, 4.0, 10.0, '不适用', 71.7],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '福州科洋医疗设备有限公司', '南', 'S2', '直服', 38.34,\n",
       "        20.0, 5.0, 7, 5.0, 10.0, '不适用', 85.34],\n",
       "       ['已完成(评估)', 2020, '策略经销商', '深圳市开源医疗器械有限公司', '南', 'S1', '直服',\n",
       "        35.13, 19.0, 3.4, 7, 3.0, 10.0, '不适用', 77.53]], dtype=object)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diabetes_array_x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d7c948a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20, 1)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diabetes_y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "801d4f55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[76.18],\n",
       "       [82.31],\n",
       "       [70.84],\n",
       "       [85.34],\n",
       "       [67.64],\n",
       "       [82.77],\n",
       "       [66.28],\n",
       "       [86.08],\n",
       "       [89.6],\n",
       "       [75.07],\n",
       "       [68.43],\n",
       "       [75.85],\n",
       "       [71.7],\n",
       "       [80.04],\n",
       "       [72.42],\n",
       "       [83.2],\n",
       "       [75.22],\n",
       "       [91.99],\n",
       "       [77.53],\n",
       "       [50.76]], dtype=object)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diabetes_X = diabetes_array_x[:, np.newaxis, 12]\n",
    "\n",
    "\n",
    "diabetes_y = diabetes_array_y[:, np.newaxis, 3]\n",
    "diabetes_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "02d31ba0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练集与测试集的划分\n",
    "# Split the data into training/testing sets\n",
    "diabetes_X_train = diabetes_X[:-10]\n",
    "diabetes_X_test = diabetes_X[-10:]\n",
    "\n",
    "diabetes_X_train\n",
    "\n",
    "# Split the targets into training/testing sets\n",
    "diabetes_y_train = diabetes_y[:-10]\n",
    "diabetes_y_test = diabetes_y[-10:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5b35c1d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coefficients: \n",
      " [[-1.32174785]]\n",
      "Mean squared error: 125.18\n",
      "Variance score: -0.21\n"
     ]
    }
   ],
   "source": [
    "# 线性回归模型的实例化\n",
    "regr = linear_model.LinearRegression()\n",
    "\n",
    "# 使用训练集训练数据\n",
    "regr.fit(diabetes_X_train, diabetes_y_train)\n",
    "\n",
    "# 调用predict接口，使用训练好的模型对测试集的自变量进行预测\n",
    "diabetes_y_pred = regr.predict(diabetes_X_test)\n",
    "\n",
    "# 线性回归的系数\n",
    "print('Coefficients: \\n', regr.coef_)\n",
    "# 均方差，mean_squared_error来源于sklearn库，前面一个是真实值，后面一个预测值\n",
    "print(\"Mean squared error: %.2f\"% mean_squared_error(diabetes_y_test, diabetes_y_pred))\n",
    "# Explained variance score: 1 is perfect prediction\n",
    "print('Variance score: %.2f' % r2_score(diabetes_y_test, diabetes_y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e86e0162",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6754946448176145"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_squared_error(diabetes_y_test, diabetes_y_pred)/diabetes_y_test.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "279b137f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAADrCAYAAABXYUzjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAI6klEQVR4nO3dvY4b5RrA8WesVO442YRU65EggIL4KPYegJ5ESBYNhQsaLsCiww03gOR+mpAbCBJU0C1CCC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JBBggiQADJBFggCQCDJBEgAFO4JFEAAk8kgggyVY8kgigizr/SCKArur8I4kAuqrzjyQC6KrOP5IIAOsBA5w7AgyQRIABkggwQBIBBkgiwABJBBggiQADJBFggCQCDJBEgAGSCDBAEgEGSCLAAEkEGCCJAAMkEWCAJAIMkESAAZIIMEASAQZIIsAASQQYIIkAAyQRYIAkAgyQRIABkggwQBIBBkgiwABJBBggiQADJBFggCQCDJBEgAGSCDBAEgEGSCLAAEkEGCCJAAMkEWCAJAIMkESAAZIIMEASAQZIIsAASQQYIIkAAyQRYIAkAgyQRIABkggwQBIBBkgiwABJBBggiQADJBFggCQCDJBEgAGSCDBAEgEGSCLAAEkEGCCJAAMkEWCAJAIMkESAAZIIMEASAQZIIsAASQQYIIkAAyQRYIAkAgyQRIABkggwQBIBBjhBVVVRlmX0er0oyzKqqmp1/5da3RvAlqiqKkajUSwWi4iImM1mMRqNIiJiOBy2cgxnwAANxuPx3/E9tFgsYjwet3YMAQZoMJ/PV9q+DgEGaLC7u7vS9nUIMECDyWQS/X7/yLZ+vx+TyaS1YwgwQIPhcBjT6TQGg0EURRGDwSCm02lrF+AiIoq6rk/94b29vXp/f7+1gwNcBEVRfF7X9d7x7c6AAZIIMEASAQZIIsAASQQYIMlKd0EURfHviJhtbjgAW+c/ERF1Xb96/A8rBRiA9piCAEgiwABJBBggiQADJBFggCQCDJBEgAGSCDBAEgEGSPJfbBPVQDU1WakAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot outputs\n",
    "plt.scatter(diabetes_X_test, diabetes_y_test, color='black')\n",
    "plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)\n",
    "\n",
    "plt.xticks(())\n",
    "plt.yticks(())\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2cb2cbf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
